home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / INHERIT3.CPP < prev    next >
Text File  |  1991-04-28  |  3KB  |  121 lines

  1.                                   // Chapter 8 - Program 3
  2. #include "iostream.h"
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    void initialize(int in_wheels, float in_weight);
  9.    int get_wheels(void) {return wheels;}
  10.    float get_weight(void) {return weight;}
  11.    float wheel_loading(void) {return weight/wheels;}
  12. };
  13.  
  14.  
  15.  
  16.  
  17. class car : vehicle {
  18.    int passenger_load;
  19. public:
  20.    void initialize(int in_wheels, float in_weight, int people = 4);
  21.    int passengers(void) {return passenger_load;}
  22. };
  23.  
  24.  
  25.  
  26.  
  27. class truck : vehicle {
  28.    int passenger_load;
  29.    float payload;
  30. public:
  31.    void init_truck(int in_wheels, float in_weight,
  32.                           int how_many = 2, float max_load = 24000.0);
  33.    float efficiency(void);
  34.    int passengers(void) {return passenger_load;}
  35. };
  36.  
  37.  
  38.  
  39.  
  40. main()
  41. {
  42. vehicle unicycle;
  43.  
  44.    unicycle.initialize(1, 12.5);
  45.    cout << "The unicycle has " << 
  46.                                 unicycle.get_wheels() << " wheel.\n";
  47.    cout << "The unicycle's wheel loading is " << 
  48.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  49.    cout << "The unicycle weighs " << 
  50.                              unicycle.get_weight() << " pounds.\n\n";
  51.  
  52. car sedan;
  53.  
  54.    sedan.initialize(4, 3500.0, 5);
  55.    cout << "The sedan carries " << sedan.passengers() << 
  56.                                                     " passengers.\n";
  57. // cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  58. // cout << "The sedan's wheel loading is " << 
  59. //                  sedan.wheel_loading() << " pounds per tire.\n\n";
  60.  
  61. truck semi;
  62.  
  63. // semi.initialize(18, 12500.0);
  64.    semi.init_truck(1, 33675.0);
  65. // cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  66.    cout << "The semi's efficiency is " << 
  67.                           100.0 * semi.efficiency() << " percent.\n";
  68. }
  69.  
  70.  
  71.  
  72.  
  73.               // initialize to any data desired
  74. void
  75. vehicle::initialize(int in_wheels, float in_weight)
  76. {
  77.    wheels = in_wheels;
  78.    weight = in_weight;
  79. }
  80.  
  81.  
  82.  
  83. void
  84. car::initialize(int in_wheels, float in_weight, int people)
  85. {
  86.    passenger_load = people;
  87.    vehicle::initialize(in_wheels, in_weight);
  88. }
  89.  
  90.  
  91.  
  92.  
  93. void
  94. truck::init_truck(int in_wheels, float in_weight,
  95.                                         int how_many, float max_load)
  96. {
  97.    vehicle::initialize(in_wheels, in_weight);
  98.    passenger_load = how_many;
  99.    payload = max_load;
  100. }
  101.  
  102.  
  103.  
  104. float
  105. truck::efficiency(void)
  106. {
  107.    return payload / (payload + vehicle::get_weight());
  108. }
  109.  
  110.  
  111.  
  112.  
  113. // Result of execution
  114. //
  115. // The unicycle has 1 wheel.
  116. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  117. // The unicycle weighs 12.5 pounds.
  118. //
  119. // The sedan carries 5 passengers.
  120. // The semi's efficiency is 41.612484 percent.
  121.